package com.app.hmst.main;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.app.hmst.model.HomestayInfoVO;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.roscopeco.ormdroid.Entity;

@SuppressLint({ "NewApi", "ValidFragment" }) public class StartEvaluateFragment extends Fragment{
	private View v;
	private SlidingMenu _sm;
	private ImageView menu_icon,login_btn,clickToNoti;
	private EditText editText1;
	public StartEvaluateFragment(SlidingMenu sm){
		this._sm = sm;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		System.gc();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		
		v = inflater.inflate(R.layout.fragment_login_evaluate_page,container, false);
		login_btn = (ImageView) v.findViewById(R.id.imageView2);
		menu_icon = (ImageView) v.findViewById(R.id.imageView6);
		editText1 = (EditText) v.findViewById(R.id.editText1);
		//Easy To Enter General Info page
		editText1.setText("93bc05");
		//Execute background process
		clickToNoti = (ImageView) v.findViewById(R.id.imageView9);
		clickToNoti.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View v) { 
				// TODO Auto-generated method stub
				switchFragment(new ViewAllEvaluatePageFragment(_sm));
			}
		});
		menu_icon.setVisibility(View.INVISIBLE); 
//		menu_icon.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				_sm.toggle();
//			}
//		});
		login_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String sumCode = editText1.getText().toString();
				new InitAndLoadData(sumCode).execute();
			}
		});		
		new Utils(getActivity()).onBackPressed(v, new HomePageFragment(_sm),null);
		return v;
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	public class InitAndLoadData extends AsyncTask<String, Void, HomestayInfoVO> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		private String sumCode;
		public InitAndLoadData(String sumCode){
			this.sumCode = sumCode;
		}
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected HomestayInfoVO doInBackground(String... params) {
			// TODO Auto-generated method stub			
			HomestayInfoVO homestayInfoVO = Entity.query(HomestayInfoVO.class).where("sumCode").eq(sumCode).execute();
			
			//Test show dialog
			//return null;
			return homestayInfoVO;
		}
		
		@Override
		protected void onPostExecute(HomestayInfoVO result) {
			super.onPostExecute(result);
			if (result != null) {
				SharedPreferences sharedPref = getActivity().getSharedPreferences("MyPref", 0);
				SharedPreferences.Editor prefEditor = sharedPref.edit();
				prefEditor.putBoolean("isLogin", true);
				prefEditor.commit();
				switchFragment(new GeneralInfoForAssessor(_sm,result));
				//switchFragment(new GeneralInfoFragment(_sm,result));
				getFragmentManager().beginTransaction().replace(R.id.menu_frame, new MenuFragment(_sm,true,result)).commit();				
				mProgressHUD.dismiss();
			} else {
				AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
	            builder1.setMessage(getString(R.string.invalid_password));
	            builder1.setCancelable(true);
	            builder1.setPositiveButton("OK",
	                    new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int id) {
	                    dialog.cancel();
	                }
	            });

	            AlertDialog alert11 = builder1.create();
	            alert11.show();
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}


	}
}
